package email

import (
	"crypto/tls"
	"gopkg.in/gomail.v2"
)

type EmailTemplate struct {
	To      string                 // 收件人
	Subject string                 // 邮件主题
	Data    map[string]interface{} // 数据
}

func (et *EmailTemplate) Render() string {
	// 简单的模板渲染
	template := `
<!DOCTYPE html>
<html>
<body>
    <h1>Hello!</h1>
    <p>Welcome to </p>
    <p>Your verification code: </p>
</body>
</html>`

	// 实际应用中可以使用 html/template 包
	return template
}

func SendTemplateEmail(template *EmailTemplate) error {
	m := gomail.NewMessage()
	m.SetHeader("From", "15055826348@163.com")
	m.SetHeader("To", template.To)
	m.SetHeader("Subject", template.Subject)
	m.SetBody("text/html", template.Render())

	//RLwjQy3PNRejb4wR
	d := gomail.NewDialer("smtp.163.com", 465, "15055826348@163.com", "RLwjQy3PNRejb4wR")

	d.TLSConfig = &tls.Config{ServerName: "smtp.163.com"}
	err := d.DialAndSend(m)
	return err
}
